package com.zgs.mall.member.service.impl;

import com.zgs.mall.member.entity.MemberLevelEntity;
import com.zgs.mall.member.vo.MemberVo;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.Query;

import com.zgs.mall.member.dao.MemberDao;
import com.zgs.mall.member.entity.MemberEntity;
import com.zgs.mall.member.service.MemberService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public void regist(MemberVo vo) {
            MemberDao memberDao =this.baseMapper;
            MemberEntity entity =new MemberEntity();
            entity.setNickname(vo.getUserName());

            //TODO 检查手机号或者用户名是否唯一  不唯一就 抛出异常
            entity.setMobile(vo.getPhone());
            entity.setUsername(vo.getUserName());
            //设置默认等级
        //盐
        long millis = System.currentTimeMillis();
        //密码进行加密存储 加盐$1$+8位字符
        //String crypt = Md5Crypt.apr1Crypt(vo.getPassword(), vo.getPassword());
        BCryptPasswordEncoder passwordEncoder =new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        //校验的
        //passwordEncoder.matches("明文","数据库中的密码");
        entity.setPassword(encode);
        memberDao.insert(entity);
    }

    @Override
    public MemberEntity login(MemberVo vo) {
        String password = vo.getPassword();
        QueryWrapper<MemberEntity> username = new QueryWrapper<MemberEntity>()
                .eq("username", vo.getUserName()).or().eq("mobile",vo.getUserName());
        MemberEntity entity = this.baseMapper.selectOne(username);
        if (entity==null){
            return null;
        }
        String passworddb = entity.getPassword();
        BCryptPasswordEncoder passwordEncoder =new BCryptPasswordEncoder();
        //明文密码和数据的库的 密文密码 比较
        boolean matches = passwordEncoder.matches(password, passworddb);
        if(matches){
            return entity;
        }
        return null;
    }
    @Transactional
    @Override
    public void seataAddtest() {
        MemberEntity MemberEntity =new MemberEntity();
        MemberEntity.setUsername("558");
        this.save(MemberEntity);
      //  throw  new RuntimeException();
    }

}